<?php


namespace app\admin\controller;

use app\admin\model\Order as Ordermodel;
use app\admin\model\OrderItem as OrderItemModel;

class Order extends Base
{
    /***
     * 展示页面
     */
    public function lists()
    {
        return $this->fetch();
    }

    /***
     * layui 数据表格接口
     */
    public function get_order_list()
    {
        $order = new Ordermodel();
        $data  = $order->get_data_list($this->_condition);
        for($i=0; $i < count($data); $i++){
            $data[$i]['nick_name']  = $data[$i]->user->nick_name;
        }
        $count = $order->get_data_count($this->_condition['where']);
        response_json('', $data, $count);
    }

    /***
     * Desc:修改订单状态
     * User: HAIYUN
     * Date: 2022/2/26 21:48
     */
    public function change_status()
    {
        $this->check_authority();
        if (!isset($this->_param['id']) || !isset($this->_param['status']))
            $this->error('参数错误');
        $order = new OrderModel();
        $res   = $order->edit(['status' => $this->_param['status']], ['uuid' => $this->_param['name']]);
        if (!$res) {
            $this->error('订单状态修改失败');
        } else {
            $str = $this->_param['status'] == 3 ? '待发货' : '已发货';
            log_write('将订单状态改为: ' . $str . ' 订单号:' . $this->_param['name'], $order->getLastSql());
            $this->success('修改订单状态成功', 'lists');
        }
    }

    /***
     * Desc:查看订单详情
     * User: HAIYUN
     * Date: 2022/2/26 21:57
     */
    public function details()
    {
        $this->check_authority();
        if (!isset($this->_param['uuid'])) $this->error('参数错误');
        $items    = new OrderItemModel();
        $order_info = $items->where('order_uuid', $this->_param['uuid'])->select();
        if (empty($order_info)) $this->error('暂无该订单详细数据');
        $res_data  = [
            'name' => '',
            'number' => '',
            'price' => '',
        ];
        foreach ($order_info as $k => $v) {
            $res_data['name']   .= ' ' . $v['title']; //商品名
            $res_data['number'] .= ' ' .$v['num'];//商品数量
            $res_data['price']  = ' ' . $v['price'];//商品单价
            $res_data['total_fee']  = ' ' . $v['total_fee'];//商品单价
        }
//        $res_data['user_phone'] = $user_info['phone'];
        log_write('查看订单详情, 订单号:' . $this->_param['uuid'], $items->getLastSql());
        $this->success('查看成功!', '', $res_data);
    }



}